///
//  LoadImageArray v 1.0
//  Russell Lowke, March 1st 2011
//
//  Copyright (c) 2011 Lowke Media
//  see http://www.lowkemedia.com for more information
//
//  Permission is hereby granted, free of charge, to any person obtaining a 
//  copy of this software and associated documentation files (the "Software"), 
//  to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, 
//  and/or sell copies of the Software, and to permit persons to whom the 
//  Software is furnished to do so, subject to the following conditions:
// 
//  The above copyright notice and this permission notice shall be included in 
//  all copies or substantial portions of the Software.
// 
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
//  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
//  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 
//  IN THE SOFTWARE. 
//
//

///
// Loads a number of image assets labeled from 0 to nImages into an Array.
//

function LoadImageArray(path,                   // (string) path to image asset
                        imageName,              // (string) name of image
                        nImages,                // (uint) number of assets to be loaded
                        type,                   // (string) file type sufix, eg ".jpg"
                        obj,                    // object to assign asset to
                        varName)                // variable on obj to assign asset to
{   
    this.asset  = new Array();                  // (array) array of images being loaded
    this.nImagesLoaded = 0;                     // (uint) number of images loaded so far
    
    // KEEP THESE PUBLIC???
    this.path       = path;
    this.imageName  = imageName;
    this.nImages    = nImages;
    this.type       = type;
    this.obj        = obj;
    this.varName    = varName;
    
    this.startLoad = function()
    {
        for (var i = 0; i < this.nImages; ++i) {
            var url = this.path + "/" + this.imageName + i + this.type;
            AssetLoader.instance.loadFile(url, this.assign, [i], this);
        }
    }
    
    this.assign = function(image,               // (object) image being assigned
                           index)               // (uint) index of image in asset array
    {
        this.asset[index] = image;
        ++this.nImagesLoaded;
        if (this.nImagesLoaded === this.nImages) {
            this.fileLoaded();
        }
    }
    
    this.fileLoaded = function()
    {
        LogAs.debug("File loaded (" + this.id + "): " + this.url, AssetLoader.DEBUG_FILE_LOADED);
        var evt = $.Event(LoadBase.ASSET_LOADED_EVENT);
        evt.asset = this.asset;
        $(this).trigger(evt);
        this.assetLoader.removeAssetLoader(this);
    }
    
    this.cleanup = function()
    {
        this.asset = null;
    }
    
    this.toString = function() {
        return "[LoadImageArray (url:" + this.url + ")]";
    }
    
    this.initialize(this.path + "/" + this.imageName + "0-" + (this.nImages - 1) + this.type);
};

LoadImageArray.prototype = new LoadBase();                  // LoadImageArray class extends LoadBase